package LLParser;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.util.ArrayList;
import java.util.Scanner;

public class Driver {

	public static void main(String[] args)
	{
		//String fileName = args[0];
//		String grammarFile = args[0];
//		String miniREFile = args[1];
        String grammarFile = "LL1Grammar.txt";
        String miniREFile = "test.txt";
		File tempFile = new File("temp.txt");
		
		Scanner scan;

		
		LL1ParserGenerator grammarParser = new LL1ParserGenerator(grammarFile);
		grammarParser.parseGrammar();
		
		MiniREParser miniREParser = new MiniREParser(miniREFile, grammarParser.getParseTable(), grammarParser.getStart(), 
			grammarParser.getRuleList(), grammarParser.getTokenSet());
		
		ParseTable pTable = grammarParser.getParseTable();
		
		ArrayList<Rule> ruleList = grammarParser.getRuleList();
		System.out.println("\nRules: ");
		for(Rule r : ruleList)
		{
			System.out.print(r.getRuleName() + " ");
			for(String s : r.getRule())
			{
				System.out.print(s);
			}
			
			System.out.println(" " + r.getRule().size());
		}
		System.out.println();
		
		try {
			//Write to temporary file and append the $;
			scan = new Scanner(new File(miniREFile));
			
			FileWriter fstream = new FileWriter(tempFile,false);
			BufferedWriter out = new BufferedWriter(fstream);
			
			while(scan.hasNextLine())
			{
				out.write(scan.nextLine() + "\n");
			}
			
			out.write("$");
			
			out.close();
			fstream.close();
			scan.close();
			
			//Read the temporary file and add to miniREParser
			scan = new Scanner(tempFile);
			
			while(scan.hasNextLine())
			{
				miniREParser.parse(scan.nextLine());
			}
			
			//Close scanner and delete temporary file.
			scan.close();
			tempFile.delete();
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
